package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.config.JavaEmailSender;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
@RequestMapping("/Mail")
public class MailController {
    Integer code = null;

    public Integer getCode() {
        return code;
    }

    @RequestMapping("email")
    @ResponseBody
    public String mailSucc(HttpServletRequest request) throws Exception {
        String toEMAIL = request.getParameter("email");         //对方邮箱
        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000); // 有一定的规范,不可随意生成随机数
        //code = mobile_code;
        String content = "您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。";
        String CONTENT = toEMAIL + "验证码发送成功";        //内容1
        JavaEmailSender.sendEmail(toEMAIL, content, CONTENT);
        return "/mailSucc";

    }

    @ResponseBody
    @RequestMapping(value = "compare", method = RequestMethod.POST)
    public Map <String, Object> compare(@RequestParam(value = "code") String mapcode) {
        System.out.println(mapcode);
        MailController mailController=new MailController();
        Integer code = mailController.getCode();
        MapUtil mapUtil=new MapUtil();
        if ((mapcode).equals(code)){
            mapUtil.put("code", 0);
            mapUtil.put("msg", "验证邮箱成功，可以继续操作");
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

        }



